%% 参数设置
Fs = 1e6;         % 采样率1MHz
Fc = 100e3;       % 载波频率100kHz
N = 1024;         % 每个信号的采样点数
num_classes = 6;  % 信号类别数
samples_per_class = 500;  % 每类样本数

%% 初始化存储矩阵
data = zeros(samples_per_class*num_classes, N+1);  % 数据+标签
t = (0:N-1)/Fs;   % 时间向量

%% 生成各类信号
for class_idx = 1:num_classes
    for sample_idx = 1:samples_per_class
        % 生成当前信号
        switch class_idx
            case 1  % AM调制
                m = 0.3 + 0.4*rand();  % 随机调制指数0.3-0.7
                fm = 1e3 + 9e3*rand(); % 随机消息频率1-10kHz
                signal = (1 + m*cos(2*pi*fm*t)) .* cos(2*pi*Fc*t);
                
            case 2  % FM调制
                beta = 1 + 9*rand();    % 随机调制指数1-10
                fm = 1e3 + 9e3*rand(); % 随机消息频率1-10kHz
                signal = cos(2*pi*Fc*t + beta*sin(2*pi*fm*t));
                
            case 3  % BPSK
                bits = randi([0 1], 1, N);         % 生成随机比特
                symbols = 2*bits - 1;              % BPSK映射
                signal = symbols .* cos(2*pi*Fc*t); % 直接调制
                
            case 4  % QPSK
                symbols = randi([0 3], 1, N);       % 生成随机符号
                phase = exp(1i*(pi/4 + symbols*pi/2)); % QPSK映射
                signal = real(phase .* exp(1i*2*pi*Fc*t));
                
            case 5  % 16QAM
                M = 16;
                symbols = qammod(randi([0 M-1], 1, N), M, 'UnitAveragePower', true);
                signal = real(symbols .* exp(1i*2*pi*Fc*t));
                
            case 6  % 64QAM
                M = 64;
                symbols = qammod(randi([0 M-1], 1, N), M, 'UnitAveragePower', true);
                signal = real(symbols .* exp(1i*2*pi*Fc*t));
        end
        
        % 归一化并存储
        signal = signal/max(abs(signal));  % 归一化到[-1,1]
        row_idx = (class_idx-1)*samples_per_class + sample_idx;
        data(row_idx, 1:N) = signal;
        data(row_idx, end) = class_idx;
    end
end

%% 保存到CSV文件
header = [strcat('S', string(1:N)), 'Label'];  % 创建列标题
csv_data = array2table(data, 'VariableNames', header);

writetable(csv_data, 'rf_signals_dataset.csv');
disp('数据已成功保存到rf_signals_dataset.csv');